package com.brillilab.starter.controller.aio.inventory;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.domain.enums.inventory.InventoryShareEnum;
import com.brillilab.domain.po.inventory.InventoryShare;
import com.brillilab.domain.vo.inventory.InventoryShareVo;
import com.brillilab.domain.vo.lab.LabMemberVo;
import com.brillilab.domain.vo.space.InventoryShareAddVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryShareService;
import com.brillilab.service.core.lab.ILabMemberService;
import com.brillilab.service.logic.inventory.InventoryShareLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wuzhiyu
 * @Title: InventoryShareController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2018/11/2110:13
 */
@RestController
@RequestMapping("/api/inventoryManage/share")
public class InventoryShareController {
    @Autowired
    private IInventoryShareService inventoryShareService;
    @Autowired
    private UsersLogic usersLogic;
    @Autowired
    private InventoryShareLogic inventoryShareLogic;
    @Autowired
    private ILabMemberService labMemberService;

    @AspectLog
    @PostMapping("/add")
    public ResponseVo add(@RequestBody InventoryShareAddVo shareVo, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        Assert.isTrue(shareVo.getInventoryIdList().size() > 0, "inventoryIdList is empty");
        Assert.notNull(shareVo.getLabMemberIdList().size() > 0, "target labMemberId is null");
        UserInfoVo info = usersLogic.getUserByToken(token);
        inventoryShareLogic.InventoryShare(shareVo, info.getLabMemberId(),info);
        return ResponseVo.success();
    }

    @AspectLog
    @GetMapping("/cancel")
    public ResponseVo cancel(Long shareId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        Assert.notNull(shareId, "shareId is null");
        InventoryShare share = inventoryShareService.get(shareId);
        Assert.notNull(share, "share not exist");
        UserInfoVo info = usersLogic.getUserByToken(token);
        if (!share.getFromLabMemberId().equals(info.getLabMemberId())) {
            throw new BrillilabException(ResultEnum.USER_NOT_POWER);
        }
        InventoryShare update = new InventoryShare();
        update.setId(shareId);
        update.setState(InventoryShareEnum.CANCELED.getState());
        inventoryShareService.updateById(share);
        return ResponseVo.success();
    }

    @GetMapping("/shareList")
    public ResponseVo shareList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        UserInfoVo info = usersLogic.getUserByToken(token);
        List<InventoryShareVo> list = inventoryShareService.myShareList(info.getLabMemberId());
        List<LabMemberVo> memList = labMemberService.selectByLabId(info.getLabId());
        list.forEach(l -> {
            LabMemberVo mem = memList.stream().filter(m -> m.getLabMemberId().equals(l.getToLabMemberId())).findFirst().get();
            l.setToName(mem.getUserName());
        });
        return ResponseVo.success(list);
    }
}
